package com.zhangzhan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.constants.SystemConstants;
import com.zhangzhan.domain.commcon.ResponseResult;
import com.zhangzhan.domain.dto.SaveMenuDto;
import com.zhangzhan.domain.dto.UpdateMenuDto;
import com.zhangzhan.domain.entity.Menu;
import com.zhangzhan.domain.vo.MenuTreeSelectVO;
import com.zhangzhan.domain.vo.RoleMenuTreeselectVO;
import com.zhangzhan.mapper.MenuMapper;
import com.zhangzhan.service.MenuService;
import com.zhangzhan.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author makejava
 * @since 2022-12-05 15:05:31
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    /**
     * @param id
     * @return java.util.List<java.lang.String>
     * @description 查询用户权限
     * @author 31115
     * @date 2022/12/5 15:16
     */
    @Override
    public List<String> selectPermsByUserId(Long id) {

        //如果id等于1代表是超级管理员直接获取所以权限信息返回
        if (id == 1) {
            LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(Menu::getMenuType, "C", "M");
            wrapper.eq(Menu::getStatus, SystemConstants.STATUS_STRING);
            wrapper.eq(Menu::getDelFlag, SystemConstants.DELFLAG_STRING);
            List<Menu> menuList = list(wrapper);
            List<String> permsList = menuList.stream()
                    .map(Menu::getPerms)
                    .collect(Collectors.toList());
            return permsList;
        }

        //如果是其他用户则根据已有权限返回
        return getBaseMapper().selectPermsByUserId(id);
    }

    /**
     * @param status
     * @param menuName
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @description 菜单数据展示
     * @author 31115
     * @date 2022/12/7 11:04
     */
    @Override
    public ResponseResult listMenu(String status, String menuName) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(menuName), Menu::getMenuName, menuName);
        wrapper.eq(StringUtils.hasText(status), Menu::getStatus, status);
        wrapper.orderByAsc(Menu::getParentId, Menu::getOrderNum);
        List<Menu> menuList = list(wrapper);
        return ResponseResult.okResult(menuList);
    }

    /**
     * @param saveMenuDto
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @description 新增
     * @author 31115
     * @date 2022/12/7 11:33
     */
    @Override
    public ResponseResult saveMenu(SaveMenuDto saveMenuDto) {
        Menu menu = BeanCopyUtils.beanCopyUtils(saveMenuDto, Menu.class);
        save(menu);
        return ResponseResult.okResult();
    }

    /**
     * @param id
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @description 修改前回显
     * @author 31115
     * @date 2022/12/7 13:45
     */
    @Override
    public ResponseResult rowBackMenu(Long id) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getId, id);
        Menu menu = getOne(wrapper);
        return ResponseResult.okResult(menu);
    }

    /**
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @description 修改菜单
     * @author 31115
     * @date 2022/12/7 13:54
     */
    @Override
    public ResponseResult updateMenu(UpdateMenuDto updateMenuDto) {
        if (updateMenuDto.getParentId().equals(updateMenuDto.getId())) {
            return ResponseResult.errorResult(500, "修改菜单" + " '" + updateMenuDto.getMenuName() + "' " +
                    "失败，上级菜单不能选择自己");
        }

        Menu menu = BeanCopyUtils.beanCopyUtils(updateMenuDto, Menu.class);

        updateById(menu);

        return ResponseResult.okResult();
    }

    /**
     * @description 删除菜单
     * @param menuId
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/7 14:17
     */
    @Override
    public ResponseResult deleteMenu(Long menuId) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getParentId,menuId);

        int count = this.count(wrapper);

        //如果等于0可以删除
        if (count == 0){
            LambdaUpdateWrapper<Menu> wrapper1 = new LambdaUpdateWrapper<>();
            wrapper1.eq(Menu::getId,menuId);
            wrapper1.setSql("del_flag = '1'");
            this.update(wrapper1);
            return ResponseResult.okResult();
        }else {
            return ResponseResult.errorResult(500,"存在子菜单不允许删除");
        }
    }

    /**
     * @description 获取菜单树接口
     * 
     * @return com.zhangzhan.domain.commcon.ResponseResult<com.zhangzhan.domain.vo.MenuTreeSelectVO>
     * @author 31115 
     * @date 2022/12/7 15:25
     */
    @Override
    public ResponseResult<MenuTreeSelectVO> treeselect() {
        List<MenuTreeSelectVO> treeSelectVO =  getBaseMapper().treeselect();
        return ResponseResult.okResult(treeSelectVO);
    }


    /**
     * @description 加载对应角色菜单列表树接口
     * @param id
     * @return com.zhangzhan.domain.commcon.ResponseResult<com.zhangzhan.domain.vo.RoleMenuTreeselectVO>
     * @author 31115
     * @date 2022/12/7 21:59
     */
    @Override
    public ResponseResult<RoleMenuTreeselectVO> roleMenuTreeselect(Long id) {

        //取树结构
        List<MenuTreeSelectVO> treeselectMap = getBaseMapper().roleMenuTreeselect();

        //取权限对应id
        List<Long> checkedKeys = getBaseMapper().checkedKeys(id);

        //封装数据
        RoleMenuTreeselectVO roleMenuTreeselectVO = new RoleMenuTreeselectVO();
        roleMenuTreeselectVO.setMenus(treeselectMap);
        roleMenuTreeselectVO.setCheckedKeys(checkedKeys);
        return ResponseResult.okResult(roleMenuTreeselectVO);
    }
}

